package com.major.crowd.service.impl;

import com.major.crowd.mapper.*;
import com.major.crowd.po.MemberConfirmInfoPO;
import com.major.crowd.po.MemberLaunchInfoPO;
import com.major.crowd.po.ProjectPO;
import com.major.crowd.po.ReturnPO;
import com.major.crowd.service.api.ProjectService;
import com.major.crowd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class ProjectServiceImpl implements ProjectService {

    @Autowired(required = false)
    private ProjectPOMapper projectPOMapper;
    @Autowired(required = false)
    private ProjectItemPicPOMapper  projectItemPicPOMapper;

    @Autowired(required = false)
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;
    @Autowired(required = false)
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Autowired(required = false)
    private ReturnPOMapper returnPOMapper;


    /**
     * 保存项目的全部信息
     * @param projectVO
     * @param memberId
     */
    @Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, Integer memberId) {

        /***************** 保存projectPO对象 *****************************/
        // 创建PO对象
        ProjectPO projectPO = new ProjectPO();
        // 复制属性
        BeanUtils.copyProperties(projectVO,projectPO);

        // bug: 把memberId设置到 projectPO中
        projectPO.setMemberid(memberId);
        // bug: 生成创建时间
        String createdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        projectPO.setCreatedate(createdate);
        // bug: status设置为0，表示即将开始
        projectPO.setStatus(0);
        // 不知道是不是bug的bug
        projectPO.setDeploydate(createdate);


        // 保存PO，为了能让主键自增，设置： <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id"
        projectPOMapper.insertSelective(projectPO);
        // 获取自增的主键
        Integer projectId = projectPO.getId();

        /***************** 保存项目、分类的关联关系信息 *********************/
        // 获取typeIdList
        List<Integer> typeIdList = projectVO.getTypeIdList();
        projectPOMapper.insertTypeRelationship(typeIdList,projectId);

        /***************** 保存项目、标签的关联关系信息 *********************/
        List<Integer> tagIdList = projectVO.getTagIdList();
        projectPOMapper.insertTagRelationship(tagIdList,projectId);

        /***************** 保存项目中详情图片信息 **************************/
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(detailPicturePathList,projectId);

        /***************** 保存项目发起人信息 *****************************/
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        BeanUtils.copyProperties(memberLauchInfoVO,memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(memberId);

        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);


        /***************** 保存项目回报信息 ******************************/
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();
        List<ReturnPO> returnPOList = new ArrayList<>();

        for (ReturnVO returnVO : returnVOList) {
            ReturnPO returnPO = new ReturnPO();
            BeanUtils.copyProperties(returnVO,returnPO);

            // 对象放进集合里
            returnPOList.add(returnPO);
        }

        returnPOMapper.insertReturnPOBatch(returnPOList,projectId);


        /***************** 保存项目确认信息 *******************************/
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
        BeanUtils.copyProperties(memberConfirmInfoVO,memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(memberId);

        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);
    }


    /**
     * 查询分类信息
     * @return
     */
    @Override
    public List<PortalTypeVO> getPortalTypeVO() {
        return projectPOMapper.selectPortalTypeVOList();
    }

    /**
     * 查询项目详情信息
     * @param projectId
     * @return
     */
    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {

        /* 计算lastday */
        // 查到 对象
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
        Integer status = detailProjectVO.getStatus();

        switch (status){
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;

            default:
                break;
        }

        // 根据createDate 计算 lastday  (这里应该是根据createDate来计算剩余日期，而不是deployDay)
         String deployDate = detailProjectVO.getDeployDate();

        // 获取当前日期
        Date currentDay = new Date();
        // 众筹日期转换为Date
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date deployDay = format.parse(deployDate);

            // 获取当前日期的时间戳
            long currentTimeStamp = currentDay.getTime();
            // 获取众筹日期的时间戳
            long deployDayTimeStamp = deployDay.getTime();
            // 时间戳相减
            long pastDays = (currentTimeStamp - deployDayTimeStamp) / 1000 / 60 / 60 / 24;

            // 获取总众筹天数
            Integer sumDays = detailProjectVO.getDay();
            // 总众筹时间 减去 过去时间
            Integer lastDays = (int) (sumDays - pastDays);

            detailProjectVO.setLastDay(lastDays);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return detailProjectVO;
    }
}
